package airplane.com.example.controller;

import airplane.com.example.entry.Ticket;
import airplane.com.example.service.TickerService;
import airplane.com.example.utilities.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/ticket")
public class TicketController {

    @Autowired
    TickerService tickerService;

    @PostMapping("/insertTicket")
    public Result insertTicket(@RequestBody Ticket ticket) {
        return tickerService.insertTicket(ticket);
    }

    @PostMapping("/deleteTicket")
    public Result deleteTicket(@RequestBody Ticket ticket) {
        return tickerService.deleteTicketByAirplane(ticket);
    }

    @GetMapping("/findByPage")
    public Map<String,Object> findByPage(Integer currentPage, Integer pageSize, String userName) {
        currentPage = currentPage == null ? 0 : currentPage;
        pageSize = pageSize == null ? 14 : pageSize;
        Map<String, Object> map = new HashMap<>();
        List<Ticket> t = tickerService.findByPage(currentPage, pageSize, userName);
        map.put("user", t);
        map.put("pageTotal", tickerService.findTotal());
        return map;
    }
}
